/**
 * TextureLoader takes the name of an images list file, and loads all images
 * from that list. images loaded can be of four different types: o - a single
 * image n - a numbered list of images s - a number of images loaded as a
 * graphics strip - such as a gif with several images in it g - a group of
 * images each with different names
 * @author Owner
 */
package com.dalonedrau.jogl.opengl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.dalonedrau.pool.PooledStringBuilder;
import com.dalonedrau.pool.StringBuilderPool;

/**
 * @author DaLoneDrau
 */
public final class TextLoader {
	/** the one and only instance of the <code>TextLoader</code> class. */
	private static TextLoader	instance;
	/**
	 * Gives access to the singleton instance of {@link TextLoader}.
	 * @return {@link TextLoader}
	 */
	public static TextLoader getInstance() {
		if (TextLoader.instance == null) {
			TextLoader.instance = new TextLoader();
		}
		return TextLoader.instance;
	}
	/** the folder containing the image library. */
	private String	libraryFolder;
	/**
	 * Loads a text file and returns the text contained therein.
	 * @param fileName the name of the file
	 * @return {@link String}
	 * @throws Exception if an error occurs
	 */
	public String loadText(final String fileName) throws Exception {
		String text = null;
		PooledStringBuilder sb =
				StringBuilderPool.getInstance().getStringBuilder();
		sb.append(libraryFolder);
		sb.append(fileName);
		String filePath = sb.toString();
		sb.setLength(0);
		// logger.info("loadImage(" + filePath + ")");
		try {
			if (libraryFolder.startsWith("com")) { // jar implementation
				InputStream is =
						getClass().getClassLoader().getResourceAsStream(
								filePath);
				BufferedReader br = new BufferedReader(
						new InputStreamReader(is));
				sb.setLength(0);
				String line = null;
				while ((line = br.readLine()) != null) {
					sb.append(line);
				}
				br.close();
				text = sb.toString();
			} else {
				File file = new File(".\\", filePath);
				if (file.exists()) {
					FileInputStream fis = new FileInputStream(file);
					BufferedReader br = new BufferedReader(
							new InputStreamReader(fis));
					sb.setLength(0);
					String line = null;
					while ((line = br.readLine()) != null) {
						sb.append(line);
					}
					br.close();
					text = sb.toString();
				} else {
					sb.setLength(0);
					sb.append("ERROR! TextLoader loadText() file ");
					sb.append(filePath);
					sb.append(" does not exist.");
					Exception ex = new Exception(sb.toString());
					sb.setLength(0);
					sb.returnToPool();
					throw ex;
				}
			}
			if (text == null) {
				sb.setLength(0);
				sb.append("ERROR! TextLoader loadText() file ");
				sb.append(filePath);
				sb.append(" cannot be read.");
				Exception ex = new Exception(sb.toString());
				sb.setLength(0);
				sb.returnToPool();
				throw ex;
			}
		} catch (Exception exception) {
			throw exception;
		} // end try-catch block
		return text;
	}
	/**
	 * Sets the location of the library folder.
	 * @param folder the location to set
	 */
	public void setLibraryFolder(final String folder) {
		PooledStringBuilder path =
				StringBuilderPool.getInstance().getStringBuilder();
		path.append(folder);
		if (folder.startsWith("com")
				&& !folder.endsWith("/")) {
			path.append("/");
		} else if (!folder.startsWith("com")
				&& !folder.endsWith("\\")) {
			path.append("\\");
		}
		libraryFolder = path.toString();
		path.returnToPool();
	}
}
